package top.whysu.rongcloud.util;

import io.rong.RongCloud;
import io.rong.messages.BaseMessage;
import io.rong.models.message.GroupMessage;
import io.rong.models.message.PrivateMessage;
import io.rong.models.message.SystemMessage;
import io.rong.models.response.ResponseResult;
import io.rong.models.user.UserModel;
import top.whysu.rongcloud.constant.ConfigConstant;

/**
 * @author whysu
 * @version 1.00.00
 * @description
 * @copyright
 * @company
 * @date 2021-03-17 16:23
 */
public class RongCloudUtil {

    private static RongCloud rongCloud = RongCloud.getInstance(ConfigConstant.appKey, ConfigConstant.appSecret);

    /**
     * 通过融云发送系统信息，详见https://www.rongcloud.cn/docs/message_architecture.html#group_notification_message
     * @param senderId 发送人
     * @param targetIds 接收人（数组）
     * @param baseMessage 消息内容
     * @param pushContent 推送内容
     * @param pushData 推送数据
     */
    public static void sendSystemMessage(String senderId, String[] targetIds, BaseMessage baseMessage, String pushContent, String pushData){
        SystemMessage systemMessage = new SystemMessage()
                .setSenderId(senderId)
                .setTargetId(targetIds)
                .setObjectName(baseMessage.getType())
                .setContent(baseMessage)
                .setPushContent(pushContent)
                .setPushData(pushData)
                .setIsPersisted(0)
                .setIsCounted(0)
                .setContentAvailable(0);
        try {
            rongCloud.message.system.send(systemMessage);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 通过融云发送单聊消息
     */
    public static void sendPrivateMsg(String senderId, String targetId, BaseMessage baseMessage){
        PrivateMessage privateMessage = new PrivateMessage()
                .setSenderId(senderId)
                .setTargetId(new String[]{targetId})
                .setObjectName(baseMessage.getType())
                .setContent(baseMessage)
                .setPushContent("")
                .setPushData("")
                .setCount("")
                .setVerifyBlacklist(0)
                .setIsPersisted(0)
                .setIsCounted(0)
                .setIsIncludeSender(0);
        //发送单聊方法
        try {
            rongCloud.message.msgPrivate.send(privateMessage);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 发送群组消息
     * @param fromUserId 注意当前用户id不会接收到该消息
     * @param toGroupId 群id
     */
    public static void sendGroupMessage(String fromUserId, String toGroupId, BaseMessage baseMessage, String pushContent, String pushData){
        GroupMessage groupMessage = new GroupMessage()
                .setSenderId(fromUserId)
                .setTargetId(new String[]{toGroupId})
                .setObjectName(baseMessage.getType())
                .setContent(baseMessage)
                .setPushContent(pushContent)
                .setPushData(pushData)
                .setIsPersisted(0)
                .setIsCounted(0)
                .setIsIncludeSender(0)
                .setContentAvailable(0);
        try {
            ResponseResult groupResult = rongCloud.message.group.send(groupMessage);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 移除黑名单
     */
    public static void removeBlackList(String userId,String friendId){
        UserModel user = getUserModel(userId,friendId);
        try {
            rongCloud.user.blackList.remove(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 添加黑名单
     */
    public static void addBlackList(String userId,String friendId){
        UserModel user = getUserModel(userId,friendId);
        try {
            rongCloud.user.blackList.add(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static UserModel getUserModel(String userId,String friendId){
        UserModel blackUser = new UserModel().setId(userId);
        UserModel[] blacklist = {blackUser};
        UserModel user = new UserModel()
                .setId(friendId)
                .setBlacklist(blacklist);
        return user;
    }
}